from django.db import IntegrityError
from django.db.models.deletion import ProtectedError
from django.http import HttpResponse
from django.shortcuts import render

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

# Create your views here.
from utils.customerutils import CustomerUtils
from utils.responsestatus import ResponseStatus
from role.models import Role
def role_page(request):
    # current_page size 接收分页参数
    if request.method == 'POST':
        # {'page': 1, 'size': 5}
        form_data = CustomerUtils.get_request_data(request)
        # 分页处理 传回去total条数 tableData数据
        role_list = Role.objects.all().order_by('-id')
        # 获取分页对象
        paginator = Paginator(role_list, form_data.get('size', 5))
        # 获取数据总条数
        total = paginator.count
        # 当前页对象
        current_page = paginator.page(form_data.get('page', 1))
        # 当前页数据 [{},{},{}]
        table_data = list(current_page.object_list.values())
        response = {
            'code': ResponseStatus.BUSINESS_SUCCESS,
            'total': total,
            'tableData': table_data,
        }
        return CustomerUtils.response_json(response)


def role_one(request, id):
    role = Role.objects.filter(id=id).first()
    response = {
        'code': ResponseStatus.BUSINESS_SUCCESS,
        'data':{
            'role': role,
        }
    }
    return CustomerUtils.response_json(response)


def role_list(request):
    role_list = Role.objects.all()
    roles = []
    for role in role_list:
        roles.append({
            'role_id': role.id,
            'role_name': role.role_name,
        })
    resp = {
        'code': ResponseStatus.BUSINESS_SUCCESS,
        'data': {
            'roles': roles
        }
    }
    return CustomerUtils.response_json(resp)

# 新增和编辑合体
# 新增不传id，编辑传id
def role_insert_edit(request):
    if request.method == 'POST':
        form_data = CustomerUtils.get_request_data(request)
        id = form_data.get('id')
        try:
            if id is not None:
                # 更新
                Role.objects.filter(id=id).update(**form_data)
                response = {
                    'code': ResponseStatus.BUSINESS_SUCCESS,
                    'message': "更新成功"
                }
                return CustomerUtils.response_json(response)
            else:
                # 新增
                role = Role(**form_data)
                role.save()
                response = {
                    'code': ResponseStatus.BUSINESS_SUCCESS,
                    'message': "新增成功"
                }
                return CustomerUtils.response_json(response)
        except IntegrityError as e:
            response = {
                'code': ResponseStatus.BUSINESS_ERROR,
                'message': str(e)
            }
            return CustomerUtils.response_json(response)


def remove_one(request, id):
    try:
        Role.objects.filter(id=id).delete()
        response = {
            'code': ResponseStatus.BUSINESS_SUCCESS,
            'message': '删除成功'
        }
        return CustomerUtils.response_json(response)
    except ProtectedError as e:
        response = {
            'code': ResponseStatus.BUSINESS_ERROR,
            'message': '角色被占用，不能删除！'
        }
        return CustomerUtils.response_json(response)

def batch_remove(request):
    if request.method == 'POST':
        # {'ids': [7, 1]}
        form_data = CustomerUtils.get_request_data(request)
        ids = form_data.get('ids')
        try:
            Role.objects.filter(id__in=ids).delete()
            response = {
                'code': ResponseStatus.BUSINESS_SUCCESS,
                'message': '删除成功'
            }
            return CustomerUtils.response_json(response)
        except ProtectedError as e:
            response = {
                'code': ResponseStatus.BUSINESS_ERROR,
                'message': '角色被占用，不能删除！'
            }
            return CustomerUtils.response_json(response)

